package spring.core.examples.java;

import java.util.ArrayList;
import java.util.List;

import spring.core.examples.xml.Couple;
import spring.core.examples.xml.Person;
import spring.core.examples.xml.Publisher;
import spring.core.examples.xml.Resources;

/**
 * The main class of the example that uses a bean from the Spring application
 * context. Note that this class is completely decoupled from Spring!
 */
public class Application {
	/**
	 * Gets a bean from the spring application context
	 * 
	 * @param beanName
	 *            the bean name
	 * @return the bean
	 */
	private static Object getBean(String beanName) {
		return CoreJavaContext.getInstance().getBean(beanName);
	}

	/**
	 * The main method.
	 * 
	 * @param args
	 *            the arguments
	 */
	public static void main(String[] args) {
		// Retrieve some beans from context. Note that singletons and prototypes
		// are retrieved.
		List<Object> myResult = new ArrayList<Object>();
		myResult.add(getBean("hein"));
		myResult.add(getBean("harry"));
		myResult.add(getBean("elke"));
		myResult.add(getBean("aCouple"));
		myResult.add(getBean("anotherCouple"));
		Couple myCouple = (Couple) getBean("anotherCouple");
		myCouple.setSpouseA(null);
		myResult.add(myCouple);
		myResult.add(getBean("internationalization"));

		// Now apply some change to some beans an see what happens.
		Person myPerson = (Person) getBean("elke");
		myPerson.setAge(40);
		myPerson = (Person) getBean("beate");
		myPerson.setAge(10);

		// Display results and note that only the third couple is modified.
		for (Object eachCouple : myResult) {
			System.out.println(eachCouple);
		}

		// Now publish a message an see what happens with two subscribers. Note
		// that publisher will be blockes until all subscribers are finished.
		// Subsribers use same transaction.
		Publisher myPublisher = (Publisher) getBean("publisher");
		myPublisher.publish("Das ist ein Rundruf!");

		// Try to get some resources using the Spring framework.
		Resources myResources = (Resources) getBean("resources");
		myResources.getMyProperties();
	}
}
